﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Reflection;
using DevelopStudio.Basic;
using DevelopStudio.Control;
using DevelopStudio.Control.Basic;

namespace DevelopStudio
{
    public partial class SettingsForm : Form
    {
        private Dictionary<TreeNode, PageControl> controlHistory = new Dictionary<TreeNode, PageControl>();

        public bool SettingChanged
        {
            get;
            private set;
        }

        public SettingsForm()
        {
            InitializeComponent();
        }

        public void InitializePageControl()
        {
            this.treeViewGuide.SelectedNode = this.treeViewGuide.Nodes[0];
            this.ChangeMainControl(this.treeViewGuide.Nodes[0]);
        }

        private void treeViewGuide_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this.ChangeMainControl(e.Node);
        }

        private void ChangeMainControl(TreeNode node)
        {
            if (node == null ||
                node.Tag == null)
            {
                return;
            }

            Type type = Type.GetType((string)node.Tag);

            if (type == null)
            {
                return;
            }

            PageControl pageControl;

            if (controlHistory.Keys.Contains(node) == true)
            {
                pageControl = controlHistory[node];
            }
            else
            {
                Assembly assembly = Assembly.GetExecutingAssembly();

                pageControl = assembly.CreateInstance(type.FullName) as PageControl;

                if (pageControl == null)
                {
                    return;
                }

                pageControl.Size = this.panelMain.Size;
                pageControl.InitializePageControl();

                controlHistory[node] = pageControl;
            }

            this.panelMain.Controls.Clear();
            this.panelMain.Controls.Add(pageControl);
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (PageControl page in this.controlHistory.Values)
                {
                    if (page.ValidatePage() == false)
                    {
                        return;
                    }
                }

                this.SettingChanged = true;

                foreach (PageControl page in this.controlHistory.Values)
                {
                    page.ApplyChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            this.buttonCancel.Text = Properties.Resources.Close;
            Utility.SaveEnvironmentSettings();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            foreach (PageControl page in this.controlHistory.Values)
            {
                if (page != null)
                {
                    page.Dispose();
                }
            }

            this.Close();
        }
    }
}
